﻿#include "MeasureView/TextBlock.h"
#include <QFontMetrics>
#include <QDebug>

namespace Qly
{
TextBlock::TextBlock()
{
    m_boundingRect.setRect(0, 0, 0, 0);
}

TextBlock::~TextBlock()
{

}

//void TextBlock::setBottomCenter(QPoint p)
//{
//    int width = m_boundingRect.width();
//    int height = m_boundingRect.height();
//    moveTo(p.x() - width / 2.0, p.y() - height - 5);
//}

//void TextBlock::setTopCenter(QPoint p)
//{
//    int width = m_boundingRect.width();
//    moveTo(p.x() - width / 2.0, p.y() - 1);
//}

void TextBlock::moveTo(double x_pix, double y_pix, double x_um, double y_um)
{
//    m_anchorPoint_pix = QPointF(x_pix, y_pix);
//    m_anchorPoint_um = QPointF(x_um, y_um);

//    int detX = x_pix - m_boundingRect.left();
//    int detY = y_pix - m_boundingRect.top();
//    m_boundingRect.moveTo(x_pix, y_pix);
//    QList<QRect>::iterator i;
//    for (i = m_textsRect.begin(); i != m_textsRect.end(); ++i)
//    {
//        i->translate(detX, detY);
//    }
    moveTo(QPointF(x_pix, y_pix), QPointF(x_um, y_um));
}

void TextBlock::moveTo(QPointF p_pix, QPointF p_um)
{
    m_anchorPoint_pix = p_pix;
    m_anchorPoint_um = p_um;
    int detX = p_pix.x() - m_boundingRect.left();
    int detY = p_pix.y() - m_boundingRect.top();
    m_boundingRect.moveBottomLeft(p_pix.toPoint());
    QList<QRect>::iterator i;
    for (i = m_textsRect.begin(); i != m_textsRect.end(); ++i)
    {
        i->translate(detX, detY);
    }
}

void TextBlock::setTextList(QStringList texts)
{
    m_texts = texts;
    updateBoundingRect();
}

void TextBlock::updateBoundingRect()
{
    //qDebug() << m_font;
    m_textsRect.clear();
    int width = 0, height = 0;
    int offsetX = m_boundingRect.left();
    int offsetY = m_boundingRect.top();
    QFontMetrics fm(m_font);
    QStringListIterator i(m_texts);
    while(i.hasNext())
    {
        QRect br = fm.boundingRect(i.next());
        br.moveBottomLeft(QPoint(offsetX, offsetY + height));
        //qDebug() << "updateBoundingRect: m_textsRect" << br;
        m_textsRect.push_back(br);
        height += br.height();
        if(br.width() > width)
        {
            width = br.width();
        }
    }
    m_boundingRect.setWidth(width);
    m_boundingRect.setHeight(height);
    //qDebug() << "updateBoundingRect: m_boundingRect" << m_boundingRect;
}

void TextBlock::setFont(const QFont &font)
{
    m_font = font;
    updateBoundingRect();
}

void TextBlock::setText(QString text)
{
    m_texts = text.split("\n");
   // qDebug() << m_texts;
    updateBoundingRect();
}

void TextBlock::translate(QPointF offset_pix, QPointF offset_um)
{
    m_anchorPoint_pix += offset_pix;
    m_anchorPoint_um += offset_um;
    QPoint offset = offset_pix.toPoint();
    m_boundingRect.translate(offset);
    QList<QRect>::iterator i;
    for (i = m_textsRect.begin(); i != m_textsRect.end(); ++i)
    {
        i->translate(offset);
       // qDebug() << "translate: m_textsRect" << *i;
    }
    // qDebug() << "translate: m_boundingRect" << m_boundingRect;
}

void TextBlock::anchorPoint(QPointF &anchorPoint_pix, QPointF &anchorPoint_um)
{
    anchorPoint_um = m_anchorPoint_um;
    anchorPoint_pix = m_anchorPoint_pix;
}

void TextBlock::draw(QPainter &p, double angle)
{
    if(m_textsRect.isEmpty()) return;
    p.save();
    QPoint bottomLeft = m_boundingRect.bottomLeft();
    p.translate(bottomLeft);
    p.rotate(angle);
    p.setPen(m_pen);
    p.setFont(m_font);

    QRect br = m_boundingRect;
    br.moveBottomLeft(QPoint(0, 0));
    p.fillRect(br, Qt::white);
    QStringListIterator i(m_texts);
    QListIterator<QRect> j(m_textsRect);


    while(i.hasNext() && j.hasNext())
    {
        QRect rect = j.next();
        rect.translate(-bottomLeft);
//        rect.moveBottomLeft(QPoint(0, 0));

        //qDebug() << "TextBlock::draw rect = " << rect;
        p.drawText(rect, Qt::AlignLeft, i.next());
    }
    p.restore();
}

void TextBlock::draw(QPainter &p)
{
    p.save();
    p.setPen(m_pen);
    p.setFont(m_font);
    p.fillRect(m_boundingRect, Qt::white);
    QStringListIterator i(m_texts);
    QListIterator<QRect> j(m_textsRect);
    while(i.hasNext() && j.hasNext())
    {
        QRect rect = j.next();
        //QPoint bottomLeft = rect.bottomLeft();
        //rect.moveBottomLeft(QPoint(0, 0));
        //p.save();
        //p.translate(bottomLeft);
        //qDebug() << "TextBlock::draw rect = " << rect;
        p.drawText(rect, Qt::AlignLeft, i.next());
//        qDebug() << m_boundingRect << rect;
        //p.restore();
    }
    p.restore();
}
} //namespace Qly
